<?php
// ------------ UserDAO ---------------------
class UserDAO {
public static function getUsers() {
global $sql;
$users = array();

$data = $sql->select("SELECT * 
                      FROM User;");

for ($i = 0; $i < count($data); $i++) {
$users[$i] = new User(
            $data[$i]->userId,
            $data[$i]->userName,
            $data[$i]->userPass,
            $data[$i]->userFirstName,
            $data[$i]->userLastName,
            $data[$i]->userPhone,
            $data[$i]->userMail,
            $data[$i]->userAddress,
            $data[$i]->userZip,
            $data[$i]->userCity,
            $data[$i]->countryId);
}
return $users;
}

public static function getUser($id) {
global $sql;

$data = $sql->select("SELECT * 
                      FROM User 
                      WHERE userId = " . $id . ";");

if(count($data) > 0){ 
return new User(
            $data[0]->userId,
            $data[0]->userName,
            $data[0]->userPass,
            $data[0]->userFirstName,
            $data[0]->userLastName,
            $data[0]->userPhone,
            $data[0]->userMail,
            $data[0]->userAddress,
            $data[0]->userZip,
            $data[0]->userCity,
            $data[0]->countryId);
}else{ 
return null;  
} 
}

public static function insertUser($user) {
global $sql;

$sql->startTransaction();
$sqlQuery = "INSERT INTO User ( 
            userName,
            userPass,
            userFirstName,
            userLastName,
            userPhone,
            userMail,
            userAddress,
            userZip,
            userCity,
            countryId)
            VALUES (" . 
            "'" . $sql->escape($user->getUserName()) . "', " . 
            "'" . $sql->escape($user->getUserPass()) . "', " . 
            "'" . $sql->escape($user->getUserFirstName()) . "', " . 
            "'" . $sql->escape($user->getUserLastName()) . "', " . 
            "'" . $sql->escape($user->getUserPhone()) . "', " . 
            "'" . $sql->escape($user->getUserMail()) . "', " . 
            "'" . $sql->escape($user->getUserAddress()) . "', " . 
            "'" . $sql->escape($user->getUserZip()) . "', " . 
            "'" . $sql->escape($user->getUserCity()) . "', " . 
            $sql->escape($user->getCountryId()) . ");";
$sql->query($sqlQuery);
$return = $sql->getLastInsertId();
$sql->commit(); 
return $return;  
}

public static function updateUser($user) {
global $sql;

$sqlQuery = "UPDATE  User SET " . 
           "userId = " . $sql->escape($user->getUserId()) . ", " . 
           "userName = '" . $sql->escape($user->getUserName()) . "', " . 
           "userPass = '" . $sql->escape($user->getUserPass()) . "', " . 
           "userFirstName = '" . $sql->escape($user->getUserFirstName()) . "', " . 
           "userLastName = '" . $sql->escape($user->getUserLastName()) . "', " . 
           "userPhone = '" . $sql->escape($user->getUserPhone()) . "', " . 
           "userMail = '" . $sql->escape($user->getUserMail()) . "', " . 
           "userAddress = '" . $sql->escape($user->getUserAddress()) . "', " . 
           "userZip = '" . $sql->escape($user->getUserZip()) . "', " . 
           "userCity = '" . $sql->escape($user->getUserCity()) . "', " . 
           "countryId = " . $sql->escape($user->getCountryId()) . 
           " WHERE userId = " . $user->getUserId() . ";";
return $sql->query($sqlQuery);
}

public static function deleteUser($user) {
global $sql;

$sqlQuery = "DELETE FROM User 
             WHERE userId = " . $user->getUserId() . ";";
return $sql->query($sqlQuery);   
}
}
?>
